<?php

namespace app\topics\controller;

class LoginController extends \app\topics\controller\TopicsController {

    public $noLogin = true;
    protected $_middle = 'topics/Login';

    public function index() {
        if (!isPost()) {
            target($this->_middle, 'middle')->meta()->export(function ($data) {
                $this->assign($data);
                $this->topicsDisplay();
            }, function ($message, $code, $url) {
                $this->errorCallback($message, $code, $url);
            });
        } else {
            $userName = request('post', 'username');
            $password = request('post', 'password');
            $remember = request('post', 'remember', 0);
            target($this->_middle, 'middle')->setParams([
                'username' => $userName,
                'password' => $password,
                'remember' => $remember,
            ])->post()->export(function ($loginData) {
                \dux\Dux::cookie()->set('topics_login', [
                    'uid' => $loginData['uid'],
                    'token' => $loginData['token']
                ], $loginData['time']);
                $this->success('账号登录成功!', $this->action ? $this->action : url('index/Index/index'));
            }, function ($message, $code, $url) {
                $this->errorCallback($message, $code, $url);
            });
        }
    }

    public function logout() {
        \dux\Dux::cookie()->del('topics_login');
        $this->redirect(url('index'));
    }

}